package com.kyzh.core.activities

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.duoyou.task.openapi.DyAdApi
import com.google.gson.Gson
import com.gushenge.atools.ui.AutoHeightImage
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.ifUse
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.AppRequest
import com.gushenge.core.requests.LiveRequest
import com.kyzh.core.MyApplication
import com.kyzh.core.R
import com.kyzh.core.pager.home.MainActivity
import com.kyzh.core.uis.showPactView
import com.kyzh.core.utils.appinit.appInit
import com.kyzh.core.utils.getChannel
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.setLanguage
import kotlinx.coroutines.*
import org.jetbrains.anko.*
import java.util.*


class LaunchActivity : BaseActivity(), ResultListener {
    lateinit var image: ImageView
    lateinit var logo: AutoHeightImage
     var launch: Job? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val window = window
        val decorView = window.decorView
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

        window.statusBarColor = Color.TRANSPARENT
        //设置底部导航栏颜色
        window.navigationBarColor = Color.TRANSPARENT
        setContentView(R.layout.activity_launch)
        initView()
        val spSurePact =
            with(getSharedPreferences("spSurePactfile", Context.MODE_PRIVATE)) {
                getBoolean("spSurePact", false)
            }

        var key = "KgpZVG2QjN6KzXIjsY_4ag=="
        AppRequest.getBaseUrl(key){
            showPactView{
                if(!this){
                    finish()
                    return@showPactView
                }
                launch = GlobalScope.launch {
                    delay(3000)
                    if (ifUse == 0) {
                        startActivity<GuideActivity>()
                    } else {
                        startActivity<MainActivity>()
                    }
                    withContext(Dispatchers.Main) {
                        finish()
                    }
                }
                if(!spSurePact)
                    with(getSharedPreferences("spSurePactfile", Context.MODE_PRIVATE).edit()){
                        putBoolean("spSurePact", true)
                    }.apply()
                if(!spSurePact){
                    this@LaunchActivity.appInit {
                        when(this) {
                            1 -> {
                                launch?.cancel()
                            }
                            2 -> {
                                launch = GlobalScope.launch {
                                    delay(3000)
                                    if (ifUse == 0) {
                                        startActivity<GuideActivity>()
                                    } else {
                                        startActivity<MainActivity>()
                                    }
                                    withContext(Dispatchers.Main) {
                                        finish()
                                    }
                                }
                            }
                        }
                    }
                    init()
                }

            }
            if(spSurePact){
                init()
            }
        }

    }

    private fun init(){
        // 初始化im
        initData()
        LiveRequest.getConfig {
            this?.apply {
                MyApplication.init(this@LaunchActivity,live_appid)
            }
        }
        setLanguage(MMKVConsts.language)
    }

    private fun initData() {
        AppRequest.launch{
            this@LaunchActivity.image.loadImage(this@LaunchActivity,image)
            this@LaunchActivity.image.setOnClickListener {
                if(MMKVConsts.ifUse!=0){
                    if (!gid.isNullOrEmpty()&&gid!="0"){
                        launch?.cancel()
                        startActivity<MainActivity>(GlobalKeys.TYPE to GlobalKeys.GAMEACTIVITY, "id" to gid)
                        finish()
                    }
                }
            }
        }
        AppRequest.appConfig1({
            val gson = Gson()
            MMKVConsts.quanzi =gson.toJson(quanzi)

            MMKVConsts.index = gson.toJson(index_top)
            MMKVConsts.languageConfig = gson.toJson(language)
            MMKVConsts.oneKeyLogin = onelogin
            MMKVConsts.qqLogin = true
            MMKVConsts.wxLogin = true
            MMKVConsts.gogid = go_gid
            if(!shiwan_appid.isNullOrEmpty()){
                DyAdApi.getDyAdApi().init(this@LaunchActivity, shiwan_appid, shiwan_key,MMKVConsts.sub);
            }
        },{
            launch?.cancel()
        })
    }
    private fun initView() {
        image = findViewById(R.id.img)
        val icon  = find<ImageView>(R.id.icon)
        icon.setImageResource(R.drawable.logo_new)
        val tvName  = find<TextView>(R.id.tvName)
        tvName.text = getString(R.string.app_name)
    }
}
